
--------------------------------------------------------------
--- Copyright：  迷你玩-海马
--- Description：向管理中心服务器收发请求  (解耦后放在commhm里，可以不用require整个center模块)
--- 功能: 向center上报数据
---       WWW_report_load     网关ma_gate\dc_gate\data_cache上报负载
---       WWW_getLoadByName   查询网关或者dc的负载值
---       WWW_fatalReport     上报一个关键事件或者致命错误
---       WWW_configReport    上报一个在用的配置文件
--------------------------------------------------------------

local require  = require;
local ngx      = ngx;
local debug    = debug;
local string   = string;
local tonumber = tonumber;
local setmetatable = setmetatable;
local pairs = pairs
local type  = type


----数据变量----
local uu         = require("modules.commhm.utils");
local log        = require("modules.commhm.log");
local ns_time    = require("modules.commhm.time");
local ns_network = require("modules.commhm.network");

local ns_auth_comm_checker = require("modules.commhm.auth_comm_checker")
local CNgxTimerWrap        = require("modules.commhm.ngxTimerWrap" )

local ns_env                 = require("modules.env.env");
local ns_const_server_config = require("modules.commhm.const_server_config");

local table = table
local table_concat =table.concat


local FOO = {
    _VERSION = '0.12.1',
    config_cache_root = '/tmp/centerConfigCache/',   ---作为客户端的本地cache地址
};


--- ma_gate dc_gate dc 远程上报一个load值，可选额外的两个值
--- server_name: 自定义服务器名 dc ma map profile
--- load_name:   data_cache ma_gate dc_gate
--- sub_type:    子类型，目前只有data_cache在用  dc dc2 dc3 dc4
--- load: 负载值
--- load1_: 附加值1
--- load2_: 附加值2
--- ver_lua:  本地代码版本
--- ver_hot:  热更版本
--- json_: 上报的json内容
function FOO.WWW_report_load( server_name_, load_name_, sub_type_, load_, load1_, load2_, ver_lua_, ver_hot_, json_ )
    local t_ = {
        cmd = 'reportLoad',
        uin = 12345,
        time = uu.now(),

        server_name = server_name_,
        load_name = load_name_,

        sub_type  = sub_type_,     --dc dc2 dc3 dc4

        load      = load_,
        load1     = load1_,
        load2     = load2_,

        ver_lua   = ver_lua_,
        ver_hot   = ver_hot_,

        env = ns_env.debug,
        worker_id = ngx.worker.id(),
    }

    if  json_ then
        t_.post_data = 1   --有json上报
    end

    t_.token = ns_auth_comm_checker.gen_comm_inner_token( t_.uin, t_.time, t_.cmd );
    local url_ext_ = ngx.encode_args( t_ )

    local report_ = function()
        for i=1, #ns_const_server_config.center_url_list do
            local url_ = ns_const_server_config.center_url_list[i] .. '?' .. url_ext_
            local ret_, body_
            if  json_ then
                ret_, body_ = ns_network.postHttpPage( url_, json_ )
            else
                ret_, body_ = ns_network.getHttpPage( url_ )
            end
            if  ret_ == 200 then
                log.day_list( "center_server_proxy", "reportLoad", ret_, body_ )
            else
                log.day_list( "center_server_proxy", "reportLoad_error", ret_, body_, url_ )
            end
        end
    end

    FOO.timer_at( 0, report_ )
end



---获得指定的负载值
function FOO.WWW_getLoadByName( server_name_, load_name_ )
    local t_ = {
        cmd = 'getLoadByName',
        uin = 12345,
        time = uu.now(),
        server_name = server_name_,
        load_name = load_name_,
        env = ns_env.debug,
    }

    t_.token = ns_auth_comm_checker.gen_comm_inner_token( t_.uin, t_.time, t_.cmd );
    local url_ext_ = ngx.encode_args( t_ )

    for i=1, #ns_const_server_config.center_url_list do
        local url_ = ns_const_server_config.center_url_list[i] .. '?' .. url_ext_
        local ret_, body_ = ns_network.getHttpPage( url_ );
        if  ret_ == 200 then
            local ret_fin_ = table.loadstring(body_);
            if  type(ret_fin_) == 'table' then
                --log.day_list( "center_server_proxy", "getLoadByName", ret_, body_ )
                return ret_fin_
            else
                log.day_list( "center_server_proxy", "getLoadByName_error_not_lua_table", ret_, body_ )
            end
        else
            log.day_list( "center_server_proxy", "getLoadByName_error", ret_, body_, url_ )
        end
    end

end



---上报严重错误或者关键事件
-- level    严重程度  0fatal > 1error > 2warn > 3info
-- key      唯一排重key
-- tt       时间间隔   ( n秒内被排重掉 )
-- value    字符串
-- ip       是否需要区分ip
function FOO.WWW_fatalReport( level_, key_, tt_, value_, ip_ )
    local t_ = {
        cmd = 'fatalReport',
        uin = 12345,
        time = uu.now(),
        env = ns_env.debug,

        level = level_ or 3,
        key = key_,
        tt = tt_,
        ip = ip_,
    }

    t_.token = ns_auth_comm_checker.gen_comm_inner_token( t_.uin, t_.time, t_.cmd );
    local url_ext_ = ngx.encode_args( t_ )

    local report_ = function()
        for i=1, #ns_const_server_config.center_url_list do
            local url_ = ns_const_server_config.center_url_list[i] .. '?' .. url_ext_
            local ret_, body_ = ns_network.postHttpPage( url_, value_, 3000 );
            if  ret_ == 200 then
                local ret_fin_ = table.loadstring(body_);
                if  type(ret_fin_) == 'table' then
                    --log.day_list( "center_server_proxy", "getLoadByName", ret_, body_ )
                else
                    log.day_list( "center_server_proxy", "getLoadByName_error_not_lua_table", ret_, body_ )
                end
            else
                log.day_list( "center_server_proxy", "getLoadByName_error", ret_, body_, url_ )
            end
        end
    end

    FOO.timer_at( 0, report_ )
end


---上报一个配置文件
-- key            唯一排重key  ma_dc map_dc2 posting_dc3
-- config_string  字符串内容
function FOO.WWW_configReport( key_, config_string_ )
    local t_ = {
        cmd    = 'reportDataComm',
        action = 'dc_client_config',
        uin    = 12345,
        time   = uu.now(),
        env    = ns_env.debug,
        key    = key_,
    }

    t_.token = ns_auth_comm_checker.gen_comm_inner_token( t_.uin, t_.time, t_.cmd );
    local url_ext_ = ngx.encode_args( t_ )

    local report_ = function()
        for i=1, #ns_const_server_config.center_url_list do
            local url_ = ns_const_server_config.center_url_list[i] .. '?' .. url_ext_
            local ret_, body_ = ns_network.postHttpPage( url_, config_string_, 3000 );
            if  ret_ == 200 then
                local ret_fin_ = table.loadstring(body_);
                if  type(ret_fin_) == 'table' then
                    log.day_list( "center_server_proxy", "configReport_ok", ngx.worker.id(), key_, ret_, body_ )
                else
                    log.day_list( "center_server_proxy", "configReport_error_table", ngx.worker.id(), key_, ret_, body_ )
                end
            else
                log.day_list( "center_server_proxy", "configReport_error", ngx.worker.id(), key_, ret_, body_, url_ )
            end
        end
    end

    FOO.timer_at( 0, report_ )
end



-- 获得一个配置文件
-- key            唯一排重key  ma_dc map_dc2 posting_dc3
function FOO.WWW_getConfigReport( key_ )
    local t_ = {
        cmd    = 'reportDataComm',
        action = 'get_dc_config',
        uin    = 12345,
        time   = uu.now(),
        env    = ns_env.debug,
        key    = key_,
    }

    t_.token = ns_auth_comm_checker.gen_comm_inner_token( t_.uin, t_.time, t_.cmd );
    local url_ext_ = ngx.encode_args( t_ )

    for i=1, #ns_const_server_config.center_url_list do
        local url_ = ns_const_server_config.center_url_list[i] .. '?' .. url_ext_
        local ret_, body_ = ns_network.getHttpPage( url_ );
        if  ret_ == 200 then
            local ret_fin_ = table.loadstring(body_);
            if  type(ret_fin_) == 'table' and ret_fin_.select and ret_fin_.server then
                --log.day_list( "center_server_proxy", "get_dc_config", ret_, body_ )
                return ret_fin_
            else
                log.day_list( "center_server_proxy", "get_dc_config_error_not_lua_table", ret_, body_ )
            end
        else
            log.day_list( "center_server_proxy", "get_dc_config_error", ret_, body_, url_ )
        end
    end

end




----------------------------------------------------------------------
---延迟执行一个任务
FOO.timer_at = function( delay_, func_, params_ )
    local timer_ = CNgxTimerWrap:new()
    return timer_:ngx_timer_at( delay_, func_,  params_ )
end


---定期执行一个任务
FOO.timer_every = function( delay_, func_, params_ )
    local timer_ = CNgxTimerWrap:new()
    return timer_:ngx_timer_every( delay_, func_, params_  )
end


return FOO;
